package us.icitap.timsportal.ui;

import us.icitap.timsportal.util.PortalNotification;
import us.icitap.timsportal.util.SessionHandler;
import us.icitap.timsportal.web.TimsPortalApp;
import us.icitap.timsportal.web.custom.PNativeButton;
import us.icitap.timsportal.web.custom.PTextField;
import us.icitap.timsportal.web.custom.PortalVerticalLayout;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;

@SuppressWarnings("serial")
public class LoginForm extends PortalVerticalLayout {

	private PNativeButton loginButton = new PNativeButton("Hyr", new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            tryToLogIn();
        }
    });
	private TextField usernameField;
	private PasswordField passwordField = new PasswordField ();
	private CustomLayout htmlTemplate = new CustomLayout("loginForm");

	public LoginForm(){
		
		loginButton.setWidth("120px");
		loginButton.setImmediate(true);
		loginButton.setClickShortcut(KeyCode.ENTER);
		usernameField = new PTextField("");
		usernameField.setRequired(true);
		usernameField.setWidth("150px");
		usernameField.focus();
		passwordField = new PasswordField ();
		passwordField.setWidth("150px");
		passwordField.setRequired(true);
		passwordField.setImmediate(true);

		htmlTemplate.setWidth("100%");
		
		htmlTemplate.addComponent(new Label("localhost"), "serverip");
		htmlTemplate.addComponent(usernameField, "username");
		htmlTemplate.addComponent(passwordField, "password");
		
		htmlTemplate.addComponent(loginButton, "loginbutton");
		
		addComponent(htmlTemplate);
	}
	
	private void tryToLogIn(){
		if (!usernameField.isValid() || !passwordField.isValid()){
			return;
		}
		String username = (String)usernameField.getValue();
		String password = (String)passwordField.getValue();
		SessionHandler sessionHandler = ((TimsPortalApp)getApplication()).getSessionHandler();
		sessionHandler.setUsername(username);
		sessionHandler.setPassword(password);
		Boolean hasError = sessionHandler.authenticateIntoDomain();
		
		if(!sessionHandler.isDomainUserAuthenticated()){
			PortalNotification.showWarning("Perdoruesi ose fjalekalimi qe ju keni futur nuk jane te sakte.", getWindow());
			passwordField.setValue("");
			passwordField.focus();
			sessionHandler.setErrorMessage(username+" :nuk ndodhet ne domain !");
			return;
		}
		
		hasError = sessionHandler.loadSimpleUser(username);
		if(!hasError){
			sessionHandler.showErrorMessage();
			return;
		} 
		
		if(sessionHandler.isDbUserAuthenticated()){
			String sessionId = ((WebApplicationContext)getApplication().getContext()).getHttpSession().getId();
			hasError = sessionHandler.saveSessionInDB(sessionId, username);
			if(!hasError){
				sessionHandler.showErrorMessage();
				return;
			}
			sessionHandler.setPortalSessionId(sessionId);
			((TimsPortalApp)getApplication()).setUser(sessionHandler.getUser());
			sessionHandler.checkUserRoles();			
		} else {
			String sessionId = ((WebApplicationContext)getApplication().getContext()).getHttpSession().getId();
			hasError = sessionHandler.saveSessionInDB(sessionId, username);
			if(!hasError){
				sessionHandler.showErrorMessage();
				return;
			}
			PortalNotification.showTrayMessage("Ju nuk keni te drejta per te perdorur sistemin TIMS.", this.getWindow());
		}
		
		((TimsPortalApp)getApplication()).loadMainPage();
		
	}

}
